#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cvfilters.h"
#include <QFileDialog>
#include <QString>
#include <QTextStream>
#include <QPixmap>
#include <QImage>
#include <QLabel>
#include <iostream>
#include <QMessageBox>
#include "dialogselvardog.h"
#include "dialogselrank.h"
#include "dialogpercentagenoise.h"
#include "dialogselgaussiannoise.h"
#include "dialogabout.h"


QString filename=NULL;
QImage filteredImage;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    QCoreApplication::setApplicationName("Computer Vision @ Beast");
    setWindowTitle("Computer Vision @ Beast");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButtonOpenImg_clicked()
{
    filename = QFileDialog::getOpenFileName(this,tr("Choose an Image File"),QDir::currentPath(),"All File (*.*);;Image pgm (*.pgm)");
    if(!filename.isEmpty())
    {

        QImage image(filename);

        ui->labelOrigImg->setPixmap(QPixmap::fromImage(image));

        ui->labelFilteredImg->setText("No Effect Applied");
        ui->labelFilteredImg->setAlignment(Qt::AlignCenter);
    }
}



void MainWindow::on_pushButtonApplyFilter_clicked()
{
    if(filename.isNull())
    {
        QMessageBox msgBox;

        msgBox.setText("No image has been selected!");
        msgBox.setInformativeText("Please select an image by clicking on the \"Open Image\" button...");
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        msgBox.exec();
    }
    else
    {
        QImage image(filename);
        if(!ui->comboBoxFilters->currentText().compare("Box"))
        {
            filteredImage=boxFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Sobel Module"))
        {
            filteredImage=sobelModuleFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Sharpening"))
        {
            filteredImage=sharpeningFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Kirsch"))
        {
            filteredImage=kirschFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Sobel Phase"))
        {
            filteredImage=sobelPhaseFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Roberts Module"))
        {
            filteredImage=robertsModuleFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Roberts Phase"))
        {
            filteredImage=robertsPhaseFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("3/9 Filter"))
        {
            filteredImage=threeNineFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Isotropic Module"))
        {
            filteredImage=isotropicModuleFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Isotropic Phase"))
        {
            filteredImage=isotropicPhaseFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Prewitt Module"))
        {
            filteredImage=prewittModuleFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Prewitt Phase"))
        {
            filteredImage=prewittPhaseFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Gaussian Filter"))
        {
            filteredImage=gaussianFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Median Filter"))
        {
            filteredImage=medianFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Laplacian Filter"))
        {
            filteredImage=laplacianFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Rank Filter"))
        {
            DialogSelRank mDial(this);
            int rankOrder=3;
            mDial.setModal(true);
            if(mDial.exec()==QDialog::Accepted)
            {
                rankOrder=mDial.getRankValue();
            }
            filteredImage=rankFilter(image,rankOrder);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("DoG (Difference of Gaussians)"))
        {
            DialogSelVarDog mDial(this);
            QVector<float> var;
            mDial.setModal(true);
            if(mDial.exec()==QDialog::Accepted)
            {
                var=mDial.getFirstValue();
            }
            filteredImage=DoGFilter(image,var.value(0),var.value(1));
            var.clear();
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Nagao Matsuyama Filter"))
        {
            filteredImage=nagaoFilter(image);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Uniform Noise"))
        {
            DialogPercentageNoise mDial(this);
            int percOfNoise=-1;
            mDial.setModal(true);
            if(percOfNoise==-1)
            {
                percOfNoise=0;
            }
            if(mDial.exec()==QDialog::Accepted)
            {
                percOfNoise=mDial.getPercOfNoise();
            }
            filteredImage=uniformNoise(image,percOfNoise);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Gaussian Noise"))
        {
            DialogSelGaussianNoise mDial(this);
            float mean=0;
            float variance=100;
            mDial.setModal(true);
            if(mDial.exec()==QDialog::Accepted)
            {
                variance=mDial.getGaussianNoiseVariance();
                mean=mDial.getGaussianNoiseMean();
            }

            filteredImage=gaussianNoise(image,mean,variance);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Salt and Pepper Noise"))
        {
            DialogPercentageNoise mDial(this);
            int percOfNoise=-1;
            mDial.setModal(true);
            if(mDial.exec()==QDialog::Accepted)
            {
                percOfNoise=mDial.getPercOfNoise();
            }
            if(percOfNoise==-1)
            {
                percOfNoise=0;
            }
            filteredImage=saltPepperNoise(image,percOfNoise);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else if(!ui->comboBoxFilters->currentText().compare("Impulse Noise"))
        {
            DialogPercentageNoise mDial(this);
            int percOfNoise=-1;
            mDial.setModal(true);
            if(mDial.exec()==QDialog::Accepted)
            {
                percOfNoise=mDial.getPercOfNoise();
            }
            if(percOfNoise==-1)
            {
                percOfNoise=0;
            }
            filteredImage=impulseNoise(image,percOfNoise);
            ui->labelFilteredImg->setPixmap(QPixmap::fromImage(filteredImage));
        }
        else
        {
            QImage nullImage;
            filteredImage=nullImage;
            ui->labelFilteredImg->setText("No Effect Applied");
            ui->labelFilteredImg->setAlignment(Qt::AlignCenter);
        }
    }
}

void MainWindow::on_pushButtonSaveImg_clicked()
{
    QString filenameWritten="";
    if(!filename.compare(""))
    {
        QMessageBox msgBox;

        msgBox.setText("No image was opened");
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        msgBox.exec();
        filenameWritten.clear();
    }
    else if(filteredImage.isNull())
    {
        QMessageBox msgBox;
        msgBox.setText("No effect has been applied");
        msgBox.setInformativeText("Please be sure to apply an effect to an image before saving and to confirm by clicking on the \"Save\" button...");
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        msgBox.exec();

    }
    else if(!ui->labelFilteredImg->text().compare("No Effect Applied"))
    {
        QMessageBox msgBox;
        msgBox.setText("No effect has been applied");
        msgBox.setInformativeText("Please be sure to apply an effect to an image before saving and to confirm by clicking on the \"Save\" button...");
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setDefaultButton(QMessageBox::Ok);
        msgBox.exec();

    }
    else
    {
        filenameWritten = QFileDialog::getSaveFileName(this,"Save File",QDir::currentPath(),"Image pgm (*.pgm)");
        if(!(filenameWritten.endsWith(".pgm") || filenameWritten.endsWith(".PGM")) && filenameWritten.compare("")!=0)
        {
            filenameWritten=filenameWritten+".pgm";
        }
        if(filteredImage.save(filenameWritten) && filenameWritten.compare("")!=0)
        {
            QMessageBox msgBox;
            msgBox.setText("The filtered image has been saved.");
            msgBox.setStandardButtons(QMessageBox::Ok);
            msgBox.setDefaultButton(QMessageBox::Ok);
            msgBox.exec();
            filenameWritten="";
        }
        else
        {
            QMessageBox msgBox;
            msgBox.setText("No image was saved");
            msgBox.setInformativeText("Please be sure to apply an effect to an image before saving and to confirm by clicking on the \"Save\" button...");
            msgBox.setStandardButtons(QMessageBox::Ok);
            msgBox.setDefaultButton(QMessageBox::Ok);
            msgBox.exec();
        }

    }
}


void MainWindow::on_actionAbout_triggered()
{
    DialogAbout mDial(this);
    mDial.setModal(true);
    mDial.exec();
}
